feat: add dynamic weight #506
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi! We have the following situation in our code:
This projector is listening to the
ContractCartItemAddedEvent
, because once that happens, an offer occupation should be made. The weight of this projector is set to 4, because otherwise this projector would run before theCartProjector
, causing this code to throw an exception because the foreign key constraint oncontract_cart_item_id
fails.I found that the previous people that worked on this project removed the
OfferOccupation
in theCartProjector
, which is weird, why not do this in theOfferOccupationProjector
. Well, it turns out that because of the weight, the removal function below would run after the removal of the cart, meaning the database fails again because of the restrict on the foreign key. This won't work:So I know there's probably a better way to implement this with events, but the issue is that I cannot really change this anymore, so I figured: why not make the weight property a function with the event in it. Now, everyone with this same situation can base the order of the projectors (and reactors) on the
StoredEvent
(andnull
when it is resetting the state).The change to the
getWeight
is completely optional, theweight
property still works.In my case, this would be the implementation: